from rest_framework import serializers

from .models import UserModel


class UserSerializer(serializers.ModelSerializer):
    """产品信息的序列化器"""
    last_login_datetime = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    create_datetime = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    update_datetime = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)

    class Meta:
        model = UserModel
        fields = '__all__'
        extra_kwargs = {
            'user_name': {
                'required': True,
                'error_messages': {
                    'required': '账号不能为空',  # 当字段是必填但未提供时的错误消息
                    'blank': '账号输入不能为空白字符',  # 当字段是空白字符串时的错误消息
                    'invalid': '账号输入无效'  # 当输入无效时的错误消息
                }
            },
            'password': {'required': False},
            'full_name': {'required': False},
            'email': {'required': False},
            'phone': {'required': False},
            'icon': {'required': False},
            'is_active': {'required': False},
            'is_admin': {'required': False},
            'login_fail_num': {'required': False},
            'locked': {'required': False},
            'last_login_datetime': {'required': False},
            'deleted': {'required': False},
            'create_username': {'required': False},
            'create_datetime': {'required': False},
            'update_username': {'required': False},
            'update_datetime': {'required': False},
        }
